package com.telecom.cms.dao;
// default package

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.telecom.cms.dto.*;
import com.telecom.cms.util.Constant;

/**
 * Data access object (DAO) for domain model class CampMenuAuth.
 * 
 * @see .CampMenuAuth
 * @author MyEclipse Persistence Tools
 */

public class CampMenuAuthDAO {
	private static List instlist=null;
	Dao dao = new DaoImpStd();

	String db = (new Constant()).getPara("DATABASE");

	public int insert(CampMenuAuth transientInstance) {
		int rs=0;
		try {
			String sql = "insert into " + db + ".CAMP_MENU_AUTH values("
						+ transientInstance.getMMenuId() + ","
						+ transientInstance.getRoleId() + ")";
			rs = dao.insert(sql);
		} catch (Exception re) {
			System.out.println("save failed:" + re);
		}
		finally
		{
			dao.closeconn();
		}
		findAllForce();
		return rs;
	}
	
	public void delete(CampMenuAuth persistentInstance) {
		String sql = "delete from " + db + ".CAMP_MENU_AUTH where M_Menu_Id="
				+ persistentInstance.getMMenuId()+" and role_id="
				+ persistentInstance.getRoleId();
		try {
			dao.delete(sql);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		findAllForce();
	}
	
	public void deleteByRole(int roleid) {
		String sql = "delete from " + db + ".CAMP_MENU_AUTH where  role_id="
				+ roleid;
		try {
			dao.delete(sql);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		findAllForce();
	}
	
	public List findAllForce() {
		String sql = "select * from " + db + ".CAMP_MENU_AUTH ";
		List list = new ArrayList();
		try {
			ResultSet rs = dao.select(sql);
			while (rs.next()) {
				CampMenuAuth sd = new CampMenuAuth();
				sd.setMMenuId(rs.getInt(1));
				sd.setRoleId(rs.getInt(2));
				list.add(sd);
			}
			if (rs != null) {
				rs.close();
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			dao.closeconn();
		}
		instlist=list;
		return list;
	}
	
	public List findAll(int roleid) {
		//String sql = "select * from " + db + ".CAMP_MENU_AUTH where role_id="+roleid;
		List list = new ArrayList();
		if(instlist==null)
			findAllForce();
		for(int i=0;i<instlist.size();i++)
		{
			CampMenuAuth sd = (CampMenuAuth)instlist.get(i);
			if(sd.getRoleId()==roleid)
				list.add(sd);
		}
		/*try {
			ResultSet rs = dao.select(sql);
			while (rs.next()) {
				CampMenuAuth sd = new CampMenuAuth();
				sd.setMMenuId(rs.getInt(1));
				sd.setRoleId(rs.getInt(2));
				list.add(sd);
			}
			if (rs != null) {
				rs.close();
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			dao.closeconn();
		}*/
		return list;
	}
}